<template>
  <el-drawer
    title="保存模板"
    size="60%"
    :visible.sync="drawerVisible"
    direction="rtl"
    :close-on-press-escape="false"
    :wrapperClosable="false"
    :destroy-on-close="true"
    ref="tagDrawer"
    @closed="clearDrawer"
    custom-class="tag-drawer"
    v-drawerResize
  >
    <div class="drawer-wrap">
      <div class="drawer-content tag-scrollbar">
        <el-form :model="editForm" label-width="120px" :rules="editFormRules" ref="editForm">
          <el-form-item prop="cover" label="覆盖旧模板">
            <el-radio-group v-model="editForm.isCover" :disabled="disabledCover">
              <el-radio :label="1">是</el-radio>
              <el-radio :label="0">否</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item prop="tmplId" label="覆盖模板名称" v-if="editForm.isCover">
            <el-select v-model="editForm.tmplId" filterable @change="changeTemp" :disabled="disabledCover">
              <el-option
                v-for="item in tempList"
                :key="item.tmplId"
                :value="item.tmplId"
                :label="item.tmplNm"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="tmplNm" label="模板名称">
            <el-input placeholder="请输入模板名称" v-model="editForm.tmplNm"></el-input>
          </el-form-item>
          <el-form-item prop="tmplCmnt" label="模板说明">
            <el-input
              type="textarea"
              placeholder="请输入模板说明"
              v-model="editForm.tmplCmnt"
              :rows="3"
              :maxlength="200"
              :show-word-limit="true"
              resize="none"
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <div class="drawer-btns">
      <el-button @click="closeDrawer">取消</el-button>
      <el-button type="primary" size="medium" @click="save">确定</el-button>
    </div>
  </el-drawer>
</template>

<script>
export default {
  props: {
    tmplType: {
      type: Number,
      default: 1
      // 1.客群画像 2.单用户画像 3.相似人群分析
    }
  },
  data() {
    return {
      drawerVisible: false,
      disabledCover: false,
      tempList: [],
      editForm: {
        isCover: 0,
        tmplId: '',
        tmplNm: '',
        tmplCmnt: '',
        tmplJson: ''
      },
      editFormRules: {
        tmplId: [{ required: true, message: '覆盖模板不能为空', trigger: 'change' }],
        tmplNm: [{ required: true, message: '模板名称不能为空', trigger: 'blur' }],
        tmplCmnt: [{ required: true, message: '模板说明不能为空', trigger: 'blur' }]
      }
    }
  },
  methods: {
    showDrawer(params) {
      if (params.tmplId) {
        this.editForm.isCover = 1
        this.editForm.tmplId = params.tmplId
        this.editForm.tmplNm = params.tmplNm
        this.editForm.tmplCmnt = params.tmplCmnt
        this.disabledCover = true
      }
      this.editForm.tmplJson = params.json || params.tmplJson
      this.getTempList()
      this.drawerVisible = true
    },
    getTempList() {
      this.$ajax({
        url: this.$ajax.setUrl(`ast/astTagImageTmpl/getAllTemp/${this.tmplType}`),
        method: 'get',
        params: this.$ajax.setParams()
      }).then(({ data }) => {
        if (data && data.code === '0') {
          this.tempList = data.data
        } else {
          this.$message.error(data.data || data.msg)
        }
      })
    },
    changeTemp(tmplId) {
      let data = this.tempList.find(item => item.tmplId === tmplId)
      this.editForm.tmplNm = data.tmplNm
      this.editForm.tmplCmnt = data.tmplCmnt
    },
    save() {
      this.$refs.editForm.validate(valid => {
        if (valid) {
          this.$ajax({
            url: this.$ajax.setUrl('ast/astTagImageTmpl'),
            method: this.editForm.isCover ? 'put' : 'post',
            data: this.$ajax.setData({
              tmplNm: this.editForm.tmplNm,
              tmplCmnt: this.editForm.tmplCmnt,
              tmplJson: this.editForm.tmplJson,
              tmplId: this.editForm.isCover ? this.editForm.tmplId : '',
              tmplType: this.tmplType
            })
          }).then(({ data }) => {
            if (data && data.code === '0') {
              this.$message.success(data.msg)
              this.$emit('callback')
              this.closeDrawer()
            } else {
              this.$message.error(data.data || data.msg)
            }
          })
        }
      })
    },
    closeDrawer() {
      this.$refs.tagDrawer.closeDrawer()
    },
    clearDrawer() {
      Object.assign(this.$data, this.$options.data.call(this))
    }
  }
}
</script>

<style lang="scss"></style>
